<template>
  <div class="main">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <div class="search">
        <el-form-item label="计划名称" prop="name">
          <el-input v-model.trim="queryParams.name" placeholder="请输入计划名称搜索" clearable/>
        </el-form-item>
        <el-form-item label="计划描述" prop="description">
          <el-input v-model.trim="queryParams.description" placeholder="请输入计划描述搜索" clearable/>
        </el-form-item>
        <el-form-item label="定时策略" prop="cron">
          <el-input v-model.trim="queryParams.cron" placeholder="请输入定时策略搜索" clearable/>
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model.trim="queryParams.createUser" placeholder="请输入创建人名称搜索" clearable/>
        </el-form-item>
      </div>
      <div class="search">
        <el-button type="primary" @click="restList(query.keywords)" icon="el-icon-search">查询</el-button>
        <el-button @click="refresh('queryForm')" icon="el-icon-refresh">重置</el-button>
      </div>
    </el-form>
    <div class="create-btn">
      <el-button type="primary" icon="el-icon-plus" @click="goCUView('新建计划')" v-permission="'plan:add'">新建计划</el-button>
    </div>
    <div class="create-btn">
      <el-table :data="tableData" height="calc(100vh - 335px)">
        <el-table-column fixed prop="name" label="计划名称"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="createUser" label="创建人" width="150"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="180"></el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button @click="openJob(scope.row)" type="text">调度执行</el-button>
            <el-button @click="goCUView('编辑计划',scope.row)" type="text"  v-permission="'plan:update'" style="margin-right: 10px">编辑</el-button>
            <el-popconfirm
              @onConfirm="handleDelete(scope.row)"
              confirm-button-text='确定'
              cancel-button-text='我再看看'
              icon="el-icon-info"
              icon-color="red"
              title="是否确认删除？"
            >
              <el-button slot="reference" type="text" v-permission="'plan:delete'">删除</el-button>
            </el-popconfirm>

<!--            <el-button @click="runPlan(scope.row)" type="text">立即执行</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageRow"
        layout="total, prev, pager, next"
        :hide-on-single-page="value"
        @pagination="getPlanList"
      />
    </div>
    <report-dialog v-if="reportVisible" :dialogVisble="reportVisible" @closeDialog="closeDialog"
                   :respData="respData"></report-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination/index";
import ReportDialog from "../compoents/reportDialog";

export default {
  name: "plan",
  components: {Pagination, ReportDialog},
  data() {
    return {
      projectId: window.sessionStorage.getItem("projectId"),
      queryParams: {
        name: "",
        cron: "",
        createUser: "",
        description: ""
      },
      tableData: [],
      query: {
        pageRow: 10,
        pageNum: 1,
        projectId: ""
      },
      value: false,
      total: 0,
      visible: false,
      title: "",
      editData: {},
      list: [],
      respData: [],
      reportVisible: false
    };
  },
  mounted() {
    this.getPlanList();
  },
  created() {
    if (!window.sessionStorage.getItem("projectId")) {
      this.$message.info("WEB测试相关的功能，需要先在统计图表模块页面选择项目！")
    }
  },
  methods: {
    refresh(queryForm) {
      this.$refs[queryForm].resetFields();
      this.getPlanList();
    },
    restList() {
      this.query.pageNum = 1;
      this.getPlanList();
      this.closeDialog()
    },
    getPlanList() {
      this.query = {...this.query, ...this.queryParams}
      this.query.projectId = this.projectId
      this.api({
        url: "/getPlanList",
        method: "post",
        data: this.query
      }).then(resp => {
        this.tableData = resp.list;
        this.total = resp.totalCount;
      });
    },
    openDialog(title, data = null) {
      this.title = title;
      this.visible = true;
      this.editData = data ? data : {}
    },
    closeDialog() {
      this.visible = false;
      this.reportVisible = false;
    },
    handleDelete(data) {
      this.api({url: `/deletePlan/${data.id}`, method: "delete"}).then((resp) => {
        this.getPlanList()
        this.$message({message: resp, type: 'success'});
      })
    },
    goCUView(title, data = null) {
      if (data) {
        sessionStorage.setItem("planId", data.id)
        sessionStorage.setItem("title", title)
      } else {
        sessionStorage.removeItem("planId")
        sessionStorage.removeItem("title")
      }
      this.$router.push({path: 'planCu', query: {title: title}})
    },
    openJob(data){
      let hostname = document.location.hostname;
      this.copy(data);
      window.open(`http://${hostname}:8081/xxl-job-admin/jobinfo`, '_blank')
    },
    copy(data) {
      this.mes = data.id
      //创建input标签
      let input = document.createElement('input')
      //将input的值设置为需要复制的内容
      input.value = this.mes;
      //添加input标签
      document.body.appendChild(input)
      //选中input标签
      input.select()
      //执行复制
      document.execCommand('copy')
      //成功提示信息
      this.$message.success('复制成功')
      //移除input标签
      document.body.removeChild(input)
    }
  }
};
</script>

<style scoped>
.main {
  height: calc(100vh - 110px);
  padding: 20px;
  background-color: white;
  margin: 20px;
}

.search {
  margin: auto;
  text-align: center
}

.create-btn {
  margin-top: 10px;
}
</style>
